package com.xinjie.www.dao.collect;

import com.xinjie.www.dao.BaseDao;
import com.xinjie.www.po.Collect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CollectDaoImpl implements CollectDao {

    //进行收藏
    @Override
    public int doCollect(Connection connection, int postId, int userId, String time) throws SQLException {
        PreparedStatement pstm = null;
        Object[] params = {postId, userId, time};
        String sql = "insert into collect (post_id,user_id,time) values(?,?,?)";
        int excute = BaseDao.excute(connection, pstm, sql, params);
        BaseDao.closeResource(null, pstm, null);
        return excute;
    }

    //获取收藏
    @Override
    public List<Collect> getCollect(Connection connection, int userId) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Collect> collects = new ArrayList<>();
        Object[] params = {userId};
        String sql = "select * from collect where user_id = ?";
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        while (rs.next()) {
            Collect collect = new Collect();
            collect.setCollectId(rs.getInt("collect_id"));
            collect.setPostId(rs.getInt("post_id"));
            collect.setUserId(rs.getInt("user_id"));
            collect.setTime(rs.getString("time"));
            collects.add(collect);
        }
        BaseDao.closeResource(null, pstm, rs);
        return collects;
    }

    //取消收藏
    @Override
    public int cancelCollect(Connection connection, int collectId) throws SQLException {
        PreparedStatement pstm = null;
        Object[] params = {collectId};
        String sql = "delete from collect where collect_id=?";
        int excute = BaseDao.excute(connection, pstm, sql, params);
        BaseDao.closeResource(null,pstm,null);
        return excute;
    }
}
